package com.corpgovernment.supplier.controller;

import com.corpgovernment.api.organization.model.user.login.ListPtUserLoginRequest;
import com.corpgovernment.api.organization.model.user.login.ListPtUserLoginResponse;
import com.corpgovernment.api.supplier.vo.AtUserLoginVo;
import com.corpgovernment.api.supplier.vo.request.AtUserLoginRequest;
import com.corpgovernment.api.supplier.vo.request.ChangePwdReq;
import com.corpgovernment.api.supplier.vo.response.AtUserLoginResponse;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.utils.Md5Util;
import com.corpgovernment.common.utils.PasswordRegUtils;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.supplier.converter.AtUserLoginConverter;
import com.corpgovernment.supplier.entity.db.AtUserLogin;
import com.corpgovernment.supplier.service.AtUserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("ptUserLogin")
@Slf4j
public class AtUserLoginController {

    @Autowired
    private AtUserLoginService atUserLoginService;

    @RequestMapping("insert")
    public JSONResult insert(AtUserLoginVo vo) {
        atUserLoginService.insert(vo);
        return JSONResult.ok();
    }

    @RequestMapping("/login")
    public JSONResult login(@RequestBody @Valid AtUserLoginRequest request) {
        AtUserLoginResponse loginResp = atUserLoginService.login(request);
        loginResp.setLoginSuccess(true);
        return JSONResult.success(loginResp);
    }

    @RequestMapping("/getMsgCode")
    public JSONResult getMsgCode(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.getMsgCode(request));
    }

    @RequestMapping("/validateMsgCode")
    public JSONResult validateMsgCode(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.validateMsgCode(request));
    }

    @RequestMapping("/confirmUid")
    public JSONResult confirmUid(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.confirmUid(request));
    }

    @RequestMapping("/validatePwd")
    public JSONResult validatePwd(BaseUserInfo userInfo, @RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.validatePwd(userInfo, request));
    }

    @RequestMapping("/updatePwd")
    public JSONResult updatePwd(BaseUserInfo userInfo, @RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.updatePwd(userInfo, request));
    }

    @RequestMapping("/getResetPwdCode")
    public JSONResult getResetPwdCode(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.getResetPwdCode(request));
    }

    @RequestMapping("/validateResetPwdCode")
    public JSONResult validateResetPwdCode(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.validateResetPwdCode(request));
    }

    @RequestMapping("/resetPwd")
    public JSONResult resetPwd(@RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.resetPwd(request));
    }

    @RequestMapping("/getBindMsgCode")
    public JSONResult getBindMsgCode(BaseUserInfo userInfo, @RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.getBindMsgCode(userInfo, request));
    }

    @RequestMapping("/submitBindPhone")
    public JSONResult submitBindPhone(BaseUserInfo userInfo, @RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.submitBindPhone(userInfo, request));
    }

    @RequestMapping("/getUnbindMsgCode")
    public JSONResult getUnbindMsgCode(BaseUserInfo userInfo) {
        return JSONResult.success(atUserLoginService.getUnbindMsgCode(userInfo));
    }

    @RequestMapping("/submitUnBindPhone")
    public JSONResult submitUnBindPhone(BaseUserInfo userInfo, @RequestBody AtUserLoginRequest request) {
        return JSONResult.success(atUserLoginService.submitUnBindPhone(userInfo, request));
    }

    @RequestMapping("deleteUserLogin")
    public JSONResult deleteUserLogin(@RequestBody AtUserLoginVo atUserLoginVo) {
        return atUserLoginService.delUserLogin(atUserLoginVo);
    }

    /**
     * 获取服务商用户
     */
    @RequestMapping("saveOrUpdateUserLogin")
    public JSONResult saveOrUpdateUserLogin(@RequestBody AtUserLoginVo atUserLoginVo) {
        return atUserLoginService.saveOrUpdateUserLogin(atUserLoginVo);
    }

    /**
     * 根据uid获取登录用户信息
     *
     * @param atUserLoginVo
     * @return
     */
    @RequestMapping("getUserLogin")
    public JSONResult<AtUserLoginVo> getUserLogin(@RequestBody AtUserLoginVo atUserLoginVo) {
        AtUserLoginVo vo = atUserLoginService.getUserLogin(atUserLoginVo);
        if (vo == null) {
            return JSONResult.errorMsg(1001, "未查到用户");
        }
        return JSONResult.success(vo);
    }

    @PostMapping("changePassword")
    public JSONResult changePassword(@RequestBody @Valid ChangePwdReq req, BaseUserInfo bui) {
	    if (req.getPassword().length() < 6) {
		    throw new CorpBusinessException(OrganizationResponseCodeEnum.PASSWORD_LENGTH_SIX_ERROR);
	    }
	    if (!PasswordRegUtils.checkPasswordComplexity(req.getPassword())) {
		    log.info("新密码校验失败:{}", req.getPassword());
		    throw new CorpBusinessException(OrganizationResponseCodeEnum.PASSWORD_FORMAT_2_ERROR);
	    }
	    AtUserLogin atUserLogin = atUserLoginService.loadUserLogin(req.getUid(), bui.getSupplierCode());
	    atUserLogin.setPwd(Md5Util.getDeepMd5Msg(String.format("%s_%s", atUserLogin.getSupplierCode(), atUserLogin.getUserId()), req.getPassword()));
	    atUserLoginService.updatePwd(atUserLogin);
	    return JSONResult.ok();
    }

    @RequestMapping("/list")
    public JSONResult list(@RequestBody @Valid ListPtUserLoginRequest request) {
        if (request == null) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.PARAMS_NULL);
        }
        if (request.getUserLogin() == null) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.USER_LOGIN_NULL);
        }
        List<AtUserLogin> atUserLoginList = atUserLoginService.list(request.getUserLogin());
        ListPtUserLoginResponse response = new ListPtUserLoginResponse();
        response.setUserLoginList(AtUserLoginConverter.INSTANCE.convert(atUserLoginList));
        return JSONResult.success(response);
    }

}
